import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/modules/recipe/data/api/recipe_collect_detail_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:casdon/modules/recipe/ui/widgets/Recipe_card_item.dart';

class RecipeModel extends RecipeCardData {
  @override
  int get sort => 0;
  final String id;
  final String name;
  final String mainImg;
  final String view;
  final String likes;
  final String collection;
  final bool isControl;

  RecipeModel({
    required this.id,
    required this.name,
    required this.mainImg,
    required this.view,
    required this.likes,
    required this.collection,
    required this.isControl,
  });

  @override
  bool get isSmart => isControl;
  @override
  String? get url => null;
  @override
  String get recipeId => id;
  @override
  String get imgUrl => mainImg.cropParams700;

  @override
  RecipeCardData placeholder() {
    throw UnimplementedError();
  }

  @override
  String get title => name;

  @override
  String get viewCount => view;

  @override
  String get count => "0";

  // 静态方法转换
  static RecipeModel fromApi(RecipeInfo response) {
    return RecipeModel(
      id: response.recipeId,
      name: response.name ?? "",
      mainImg: (response.ossUrls ?? "").isEmpty
          ? recipePlaceHolderImgUrl
          : (response.ossUrls ?? "").split(",").first,
      view: "0",
      likes: "0",
      collection: "0",
      isControl: false,
    );
  }

  // 批量转换的静态方法
  static List<RecipeModel> listFromApi(List<RecipeInfo> items) {
    return items
        .where((item) => item.status == 1) // 过滤掉下架/隐藏菜谱
        .map(fromApi)
        .toList();
  }
}
